//
//  JCViewController.m
//  JCTagListView
//
//  Created by lijingcheng on 07/03/2015.
//  Copyright (c) 2014 lijingcheng. All rights reserved.
//

#import "JCViewController.h"

@interface JCViewController ()<UIAlertViewDelegate>

@end

@implementation JCViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [ViewFactory setNavigationBlack:self];
    self.navigationItem.titleView = [ViewFactory makeNavigationTitle:@"选择标签"];
    
    UIBarButtonItem *backBtn = [ViewFactory makeNavigationBackWhite:self action:@selector(goPop)];
    self.navigationItem.leftBarButtonItem = backBtn;
    
    UIButton *button = [ViewFactory makeNavigationTextButtonWhite:@"创建"];
    [button addTarget:self action:@selector(AddClicked) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = rightButton;
}

- (void)returnText:(ReturnTextBlock)block {
    self.returnTextBlock = block;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
    NSString *string = [self.tagListView.selectedTags componentsJoinedByString:@","];
//    if (self.returnTextBlock != nil) {
    
        self.returnTextBlock(string);
//    }
}
- (void)AddClicked{
    // 准备初始化配置参数
    NSString *title = @"创建标签";
    NSString *message = @"请创建新的部落标签";
    NSString *okButtonTitle = @"创建";
    NSString *canceButtonTitle = @"取消";
    // 初始化
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    // 创建文本框
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
        textField.placeholder = @"输入标签名称";
        textField.secureTextEntry = NO;
    }];
    // 取消操作
    UIAlertAction *canceAction = [UIAlertAction actionWithTitle:canceButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        alert.textFields.firstObject.text = @"";
    }];
    // 创建操作
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:okButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        // 读取文本框的值显示出来
        UITextField *textField = alert.textFields.firstObject;
        NSLog(@"%@",textField.text);
        if ([textField.text isEqualToString:@""]) {
            return ;
        }
        [self.tagListView.tags addObject:textField.text];
        [self.tagListView.collectionView reloadData];
        
    }];
    
    // 添加操作（顺序就是呈现的上下顺序）
    [alert addAction:canceAction];
    [alert addAction:okAction];
    
    // 呈现警告视图
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self loadNetworkData];
    [self createUI];
    
}
- (void)goPop {
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 删除标签

- (void)createUI{
    _tagListView = [[JCTagListView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tagListView];
    self.tagListView.canSelectTags = YES;
    self.tagListView.tagSelectedTextColor = [UIColor whiteColor];
    self.tagListView.tagCornerRadius = 5.0f;
    [self.tagListView.tags addObjectsFromArray:self.dataSource];
    [self.tagListView.selectedTags addObjectsFromArray:self.selectedArr];
    
    __weak __typeof(self)weakSelf = self;
    [self.tagListView setCompletionBlockWithSelected:^(NSInteger index) {
        NSLog(@"______%ld______", (long)index);
    
       NSString *tmpString =  [weakSelf.tagListView.tags objectAtIndex:index];
    }];
    
    NSLog(@"\n\n🍀🍀🍀 The warnings in the console can be ignored, the actual use of lib will not exist. 🍀🍀🍀");
}
ARRAY_INIT(dataSource)
ARRAY_INIT(selectedArr)
@end
